import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:zy_pos_demo/appinfo.dart';
import 'package:zy_pos_demo/datarepository/bean/login/loginbeans.dart';
import 'package:zy_pos_demo/datarepository/local/spdata.dart';
import 'package:zy_pos_demo/datarepository/remote/remote.dart';
import 'package:zy_pos_demo/pages/checkpage.dart';
import 'package:zy_pos_demo/pages/mainpage.dart';
import 'package:zy_pos_demo/resources/res.dart';
import 'package:zy_pos_demo/widget/inputedittexts.dart';
import 'package:zy_pos_demo/widget/statusbarspace.dart';

class LoginPage extends StatelessWidget {
  static LoginBean loginInfo;

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      home: new LoginPageWidget(),
      onGenerateRoute: (RouteSettings settings) {
        String name = settings.name;
        switch (name) {
          case "/main":
            PageRouteBuilder pageRoute = PageRouteBuilder(
                pageBuilder: (BuildContext context, Animation animation,
                    Animation secondAnimation) {
                  return FadeTransition(
                    opacity: animation,
                    child: MainPage(),
                  );
                },
                transitionDuration: Duration(milliseconds: 500));
            return pageRoute;
          case "/check":
            PageRouteBuilder checkRoute = PageRouteBuilder(
              transitionDuration: Duration(milliseconds: 500),
              transitionsBuilder:
                  (_, Animation<double> animation, __, Widget child) =>
                      FadeTransition(
                opacity: animation,
                child: RotationTransition(
                  child: child,
                  turns: Tween<double>(begin: 0.0, end: 1.0).animate(animation),
                ),
              ),
              pageBuilder: (context, _, __) => CheckPage(),
            );
            return checkRoute;
        }
        return null;
      },
    );
  }
}

class LoginPageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new LoginPageState();
  }
}

class LoginPageState extends State<LoginPageWidget> {
  TextEditingController accountController = TextEditingController();
  TextEditingController pwdController = TextEditingController();
  String _version;
  Dio _dio = new Dio();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    pwdController.addListener(() {
      setState(() {});
    });
    SPHelper().getUserAccount().then((String acount) {
      setState(() {
        accountController.text = acount == null ? "" : acount;
      });
    });
    AppInfoHelper().getAppVersion().then((v) {
      setState(() {
        _version = v;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primaryColor: Color.fromARGB(255, 153, 204, 0),
      ),
      debugShowCheckedModeBanner: false,
      home: new Scaffold(
        body: TopAreaWidget(
          child: Container(
            width: double.infinity,
            height: double.infinity,
            child: Flex(
              direction: Axis.vertical,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(top: 40, bottom: 15),
                  width: 150,
                  height: 105,
                  child: Image(
                    image: AssetImage(AppImages.LOGO),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(horizontal: 15.0),
                  child: EdgeStateEditText(
                    image: AppImages.ACCOUNT,
                    hint: "请输入账号",
                    controller: accountController,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.all(15.0),
                  child: EdgeStateEditText(
                    image: AppImages.PASSWORD,
                    hint: "请输入密码",
                    hide: true,
                    controller: pwdController,
                  ),
                ),
                Container(
                  height: 50,
                  width: double.infinity,
                  margin: EdgeInsets.all(15.0),
                  child: FlatButton(
                    child: Text(
                      "登录",
                      style: TextStyle(fontSize: 24.0, color: Colors.white),
                    ),
                    color: AppColors.ORANGE_63,
                    disabledColor: AppColors.GREY_C0,
                    highlightColor: AppColors.ORANGE_4E,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(5.0),
                    ),
                    onPressed: pwdController.text.length > 0 ? _login : null,
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    padding: EdgeInsets.only(bottom: 20.0),
                    child: Text(
                      "v:$_version",
                      style:
                          TextStyle(fontSize: 20.0, color: AppColors.BLACK_3),
                    ),
                    alignment: Alignment.bottomCenter,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  ///登录逻辑
  _login() {
    String account = accountController.text;
    if (account != null) {}
    DioHttpHelper()
        .login(account: account, pwd: pwdController.text)
        .then((loginInfo) {
      //存储到数据库然后跳页
      _operateLoginInfo(loginInfo);
    });
  }

  void _operateLoginInfo(LoginBean loginInfo) {
    LoginPage.loginInfo = loginInfo;
    Navigator.of(context).pushNamed("/main");
  }
}
